import axios from 'axios'

const httpReadInfo = async function () {
  const webUrl = "http://127.0.0.1:30004/api/info";
  return await axios.get(webUrl).then(({data}) => {
    const _idCardNo = data.CardInfo.No;
    const _userName = data.CardInfo.Name;
    const _photo = 'data:image/jpg;base64,' +  data.BmpInfo;
    return {success: true,_userName, _idCardNo, _photo}
  }).catch(e => {
    return {success: false,error:e}
  });
  
}

const conWS = function () {
  const webUrl = "ws://127.0.0.1:30004/ws";
  let ws = new WebSocket(webUrl);
  ws.onclose = function(evt) {
  };
  return ws;
}

const hex2a = function (hex) {
  let str_list = "";
  for (let i = 0; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2) {
    const a = hex.charCodeAt(i);
    const b = hex.charCodeAt(i + 1);
    const c = b * 256 + a;
    str_list += String.fromCharCode(c);
  }
  
  return str_list.toString();
}
export {httpReadInfo, hex2a, conWS};